import request from '../../api/index'
import router from '../../router'
import { Message } from "element-ui";
const login = {
    state: {
        username: '',
        token: ''
    },
    mutations: {
        SET_username(state, data) {
            state.username = data.userInfo.username
            state.token = data.token
            localStorage.setItem('userInfo', JSON.stringify(data.userInfo))
            localStorage.setItem('token', data.token)
            router.push('/')
        }
    },
    actions: {
        actChangeName(context, msg) {
            let params = {
                username: msg.username,
                password: msg.password
            };
            return new Promise((resolve, reject) => {
                request.getLogin(params).then(res => {
                    if (res.data.code == 200) {
                        resolve(res)
                        Message({ type: 'success', message: res.data.message, duration: 1000 })
                        context.commit('SET_username', res.data.result);
                    }
                    resolve(res)
                }).catch(err => {
                    reject(err)
                });
            })
        }
    },
    modules: {
    }
}
export default login